package com.originskyseed.platform.course.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.course.i18n.CourseResponseMsg;
import com.originskyseed.platform.course.model.PaperRelateAnswer;
import com.originskyseed.platform.course.service.IPaperRelateAnswerService;
import com.originskyseed.platform.course.vo.request.PaperRelateAnswerRequestVO;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 试卷关联作答信息Controller类（OSI_PAPER_RELATE_ANSWER）
 *
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/paperRelateAnswer")
@BaseLocaleResource(localeFileName = "i18n_course")
public class PaperRelateAnswerController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PaperRelateAnswerController.class);

	@DubboReference
	private IPaperRelateAnswerService paperRelateAnswerService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/paperRelateAnswerList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PaperRelateAnswer>> paperRelateAnswerList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PaperRelateAnswer>> result = null;
		try {
			// 请求对象
			PaperRelateAnswerRequestVO requestVO = this.getParameterBean(PaperRelateAnswerRequestVO.class);

			// 分页查询
			BasePage<PaperRelateAnswer> page = new BasePage<PaperRelateAnswer>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.paperRelateAnswerService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<PaperRelateAnswer>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PaperRelateAnswer>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/paperRelateAnswerSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<PaperRelateAnswer> paperRelateAnswerSave(HttpServletRequest request) {
		BaseResponseBody<PaperRelateAnswer> result = null;
		try {
			// 请求对象
			PaperRelateAnswer paperRelateAnswer = this.getParameterBean(PaperRelateAnswer.class);

			if (StringUtils.isNotBlank(paperRelateAnswer.getId())) {
				this.paperRelateAnswerService.updateByPrimaryKey(paperRelateAnswer);
			} else {
				this.paperRelateAnswerService.insertSelective(paperRelateAnswer);
			}
			result = new BaseResponseBody<PaperRelateAnswer>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.SAVE_SUCCESS.getMsg()), paperRelateAnswer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PaperRelateAnswer>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 考卷作答
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/paperRelateToAnswerSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<PaperRelateAnswer> paperRelateToAnswerSave(HttpServletRequest request) {
		BaseResponseBody<PaperRelateAnswer> result = null;
		try {
			// 请求对象
			// PaperRelateAnswer paperRelateAnswer = this.getParameterBean(PaperRelateAnswer.class);
			List<PaperRelateAnswer> paperRelateAnswerList = this.getParameterList("paperRelateAnswerList", null);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (paperRelateAnswerList != null && paperRelateAnswerList.size() > 0) {
				for (PaperRelateAnswer relateAnswer : paperRelateAnswerList) {
					// 查询是否存在
					PaperRelateAnswer paperRelateAnswer1 = new PaperRelateAnswer();
					paperRelateAnswer1.setPaperId(relateAnswer.getPaperId());
					paperRelateAnswer1.setStemId(relateAnswer.getStemId());
					paperRelateAnswer1.setCreatorId(userInfo.getId());
					PaperRelateAnswer paperRelateAnswer = this.paperRelateAnswerService.selectPaperRelateAnswer(relateAnswer);
					// 更新
					if (paperRelateAnswer != null) {
						this.paperRelateAnswerService.updateByPrimaryKeySelective(relateAnswer);
					} else {
						// 插入
						relateAnswer.setCreatorId(userInfo.getId());
						relateAnswer.setCreator(userInfo.getName());
						this.paperRelateAnswerService.insertSelective(relateAnswer);
					}
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.PAPER_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.PAPER_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/paperRelateAnswerDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> paperRelateAnswerDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.paperRelateAnswerService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CourseResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
